package com.shaoshaohuo.app.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.JPushEntity;
import com.shaoshaohuo.app.ui.GrabOrderActivity;
import com.shaoshaohuo.app.ui.MyPriceActivity;
import com.shaoshaohuo.app.ui.OrderDetailActivity;
import com.shaoshaohuo.app.ui.OrderManagerActivity;
import com.shaoshaohuo.app.ui.ec.PurchaseOrderDetailActivity;
import com.shaoshaohuo.app.ui.ec.SupplyOrderDetailActivity;
import com.shaoshaohuo.app.ui.shipper.OrderDetailShipperActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * 极光推送
 * 
 * @author cao
 *
 */
public class JPushReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		Bundle bundle = intent.getExtras();
		String action = intent.getAction();
		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			// 接收Registration Id
		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			// 接收到推送下来的自定义消息:
		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			// 接收到推送下来的通知
			//cn.jpush.android.PUSH_ID = 956487860____cn.jpush.android.ALERT = sdfdsfdadsf____cn.jpush.android.EXTRA = {"type":"http","txt":"","Data":{"title":"","url":"","viewid":"","datetime":"2015-12-11 12:55:53","message":"sdfdsfdadsf","type":"http"}}____cn.jpush.android.NOTIFICATION_ID = null____cn.jpush.android.NOTIFICATION_CONTENT_TITLE = 田贸e通____cn.jpush.android.MSG_ID = 956487860____
//			String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//			String content = bundle.getString(JPushInterface.EXTRA_ALERT);
//			String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//			int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//			String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
//			String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);
//			String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);
		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			// 用户点击打开了通知
			//cn.jpush.android.PUSH_ID = 2671402345____cn.jpush.android.NOTIFICATION_TYPE = 0____app = com.shaoshaohuo.app____cn.jpush.android.ALERT = sdfdsfdadsf____cn.jpush.android.EXTRA = {"type":"http","txt":"","Data":{"title":"","url":"","viewid":"","datetime":"2015-12-11 12:59:56","message":"sdfdsfdadsf","type":"http"}}____cn.jpush.android.NOTIFICATION_ID = null____cn.jpush.android.NOTIFICATION_CONTENT_TITLE = 田贸e通____cn.jpush.android.MSG_ID = 2671402345____
			openPush(context,bundle);
		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity，
			// 打开一个网页等..
		} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {

		} else {
		}
	}
	
	public void openPush(Context context,Bundle bundle){
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		Gson gson = new Gson();
		JPushEntity push = gson.fromJson(extras, JPushEntity.class);
		if(push==null||push.data==null||push.data.viewid==null||push.data.message==null||push.data.message.id==null){
			return;
		}
		
		String id = push.data.message.id;
		
		switch (push.data.viewid) {
		
//		1. 内容：您的发货订单收到一条车主抢单信息
//		 业务：我要发货的信息被司机抢单，发出通知推送
//		 viewid->send_grabbed
//		 id->sh_order_list    我要发货订单的orderid
		case "send_grabbed":
			Intent intent1 = new Intent(context,OrderDetailShipperActivity.class);
			intent1.putExtra(ExtraName.orderid, id);
			intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent1);
			break;
			
//			2. 内容：货主已确认装货，请发货
//			 业务：装货后，货主确认装货，通知推送给车主
//			 viewid->shipper_sended
//			 id->sh_order_list    我要发货订单的orderid
		case "shipper_sended":
			Intent intent2 = new Intent(context, OrderDetailActivity.class);
			intent2.putExtra(ExtraName.orderid, id);
			intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent2);
			break;
			
//			3. 内容：买主确认收货，请对货主评价
//			 业务：买主收货后，推送给车主去给货主评价
//			 viewid->buyer_taken
//			 id->sh_order_list    我要发货订单的orderid
		case "buyer_taken":
			Intent intent3 = new Intent(context, OrderDetailActivity.class);
			intent3.putExtra(ExtraName.orderid, id);
			intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent3);
			break;
			
//			4. 内容：车主已装车，请确认
//			 业务：车主装车后，推送给货主知晓
//			 viewid->driver_loaded
//			 id->sh_order_list    我要发货订单的orderid
		case "driver_loaded":
			Intent intent4 = new Intent(context,OrderDetailShipperActivity.class);
			intent4.putExtra(ExtraName.orderid, id);
			intent4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent4);
			break;
			
//			5. 内容：车主已完成送货，请对车主评价
//			 业务：车主送货结束后，推送给货主请对车主评价
//			 viewid->driver_end
//			 id->sh_order_list    我要发货订单的orderid
		case "driver_end":
			Intent intent5 = new Intent(context,OrderDetailShipperActivity.class);
			intent5.putExtra(ExtraName.orderid, id);
			intent5.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent5);
			break;
			
//			6. 内容：货主已付款，请发车
//			 业务：货主付款后，推送给车主，提醒发车
//			 viewid->shipper_payed
//			 id->sh_order_list    我要发货订单的orderid
		case "shipper_payed":
			Intent intent6 = new Intent(context, OrderDetailActivity.class);
			intent6.putExtra(ExtraName.orderid, id);
			intent6.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent6);
			break;
			
//			7. 内容：买家已付款，请发货
//			 业务：买家付款后，推送给卖家货主提醒发货
//			 viewid->buyer_payed
//			 id->tb_supply_order    供应订单的orderid
		case "buyer_payed":
			Intent intent7 = new Intent(context, SupplyOrderDetailActivity.class);
			intent7.putExtra(ExtraName.orderid, id);
			intent7.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent7);
			break;
			
//			8. 内容：货物已收到，请评价
//			 业务：买家收货后，推送给卖家，提醒评价买家
//			 viewid->buyer_end
//			 id->tb_supply_order    供应订单的orderid
		case "buyer_end":
			Intent intent8 = new Intent(context, SupplyOrderDetailActivity.class);
			intent8.putExtra(ExtraName.orderid, id);
			intent8.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent8);
			break;
			
//			9. 内容：卖家已发货，请耐心等待
//			 业务：卖家发货后，推送给买家等待
//			 viewid->seller_sended
//			 id->tb_supply_order    供应订单的orderid
		case "seller_sended":
			Intent intent9 = new Intent(context, PurchaseOrderDetailActivity.class);
            intent9.putExtra(ExtraName.orderid, id);
            intent9.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent9);
			break;
			
//			10. 内容：请收货，并对卖家评价
//			 业务：请收货，并对卖家评价
//			 viewid->seller_wait
//			 id->tb_supply_order    供应订单的orderid
		case "seller_wait":
			Intent intent10 = new Intent(context, PurchaseOrderDetailActivity.class);
            intent10.putExtra(ExtraName.orderid, id);
            intent10.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent10);
			break;
			
//			11. 内容：有货主发货了，快去查看吧
//			 业务：有货主发货后，通知所有车主
//			 viewid->shipper_publish
//			 id->tb_supply_order    供应订单的orderid
		case "shipper_publish":
			Intent intent11 = new Intent(context, GrabOrderActivity.class);
            intent11.putExtra(ExtraName.orderid, id);
            intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent11);
			break;

//			1 货主给车主下单  跳转至我的报价
//			内容：有货主给您下单了，请查看
//			业务：货主选择了该车主，通知车主
//			viewid->shipper_choose_driver
		case "shipper_choose_driver":
			Intent intent12 = new Intent(context, MyPriceActivity.class);
			intent12.putExtra(ExtraName.orderid, id);
			intent12.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent12);
			break;

//			2 货主删除了此条货源信息 无跳转
//
//			内容：货主已删除订单
//			业务：货主取消订单，提醒车主
//			viewid->shipper_canceld
			case "shipper_canceld":

				break;

//			1 货主选择此车主后  跳转至我的报价
//			内容：货主已选择您为物流车主，请等待支付
//			业务：货主选择了该车主，通知车主
//			viewid->shipper_choose_you
			case "shipper_choose_you":
				Intent intent13 = new Intent(context, MyPriceActivity.class);
				intent13.putExtra(ExtraName.orderid, id);
				intent13.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent13);
				break;

//			2 货主取消报价后    跳转至我的报价     是取消订单吧
//			内容：货主已取消订单
//			业务：货主取消订单，提醒车主
//			viewid->shipper_cancel
			case "shipper_cancel":
				Intent intent14 = new Intent(context, MyPriceActivity.class);
				intent14.putExtra(ExtraName.orderid, id);
				intent14.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent14);
				break;
//			4 货主提醒发货后    跳转至我的订单
//			内容：货主提醒发货
//			业务：货主提醒车主发货
//			viewid->shipper_remain
			case "shipper_remain":
				Intent intent15 = new Intent(context, OrderManagerActivity.class);
				intent15.putExtra(ExtraName.orderid, id);
				intent15.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent15);
				break;
		}
	}

}
